﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Restaurant.Modules.REST.Utility;
using Sector7.Restaurant.CoreType.Enums;
using Sector7.Restaurant.Modules.REST.LocalService;
using Sector7.Framework.Utility;
using Sector7.Framework.Enums;

namespace Sector7.Restaurant.Modules.REST.Windows
{
    /// <summary>
    /// EmployeeSummaryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class EmployeeSummaryWindow : UserControl
    {
        private List<RS_EmployeeGroup> _groups = null;

        public EmployeeSummaryWindow()
        {
            InitializeComponent();
        }

        private void InitializeData()
        {
            _groups = Aide.LocalClient.Invoke<List<RS_EmployeeGroup>>(
                service => service.GetAllEmployeeGroups(true));

            ClientUtil.LoadItemsSource(dtgGroup, _groups);
        }

        private void btnAddGroup_Click(object sender, RoutedEventArgs e)
        {
            AddEmployeeGroupWindow addEmployeeGroupWindow = new AddEmployeeGroupWindow();
            addEmployeeGroupWindow.InitializeWindow(CrudType.Create);
            addEmployeeGroupWindow.AddSuccess += new EventHandler(addEmployeeGroupWindow_AddSuccess);

            addEmployeeGroupWindow.ShowDialog();
        }

        private void addEmployeeGroupWindow_AddSuccess(object sender, EventArgs e)
        {
            AddEmployeeGroupWindow addEmployeeGroupWindow = sender as AddEmployeeGroupWindow;
            RS_EmployeeGroup group = addEmployeeGroupWindow.EmployeeGroup;
            _groups.Add(group);

            ClientUtil.LoadItemsSource(dtgGroup, _groups);

            addEmployeeGroupWindow.ResetWindow();
        }

        private void btnModifyGroup_Click(object sender, RoutedEventArgs e)
        {
            AddEmployeeGroupWindow modifyEmployeeGroupWindow = new AddEmployeeGroupWindow();
            modifyEmployeeGroupWindow.EmployeeGroup = dtgGroup.SelectedItem as RS_EmployeeGroup;
            modifyEmployeeGroupWindow.InitializeWindow(CrudType.Update);
            modifyEmployeeGroupWindow.AddSuccess += new EventHandler(modifyEmployeeGroupWindow_AddSuccess);

            modifyEmployeeGroupWindow.ShowDialog();
        }

        private void modifyEmployeeGroupWindow_AddSuccess(object sender, EventArgs e)
        {
            var modifyEmployeeGroupWindow = sender as AddEmployeeGroupWindow;
            RS_EmployeeGroup newGroup = modifyEmployeeGroupWindow.EmployeeGroup;
            _groups.Find(delegate(RS_EmployeeGroup group)
            {
                if (group.GroupId == newGroup.GroupId)
                {
                    group = newGroup;
                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgGroup, newGroup);
        }

        private void btnDeleteGroup_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnAddEmployee_Click(object sender, RoutedEventArgs e)
        {
            RS_EmployeeGroup selectedGroup = dtgGroup.SelectedItem as RS_EmployeeGroup;

            if (null != selectedGroup)
            {
                AddEmployeeWindow addEmployeeWindow = new AddEmployeeWindow(selectedGroup, _groups);
                addEmployeeWindow.InitializeWindow(CrudType.Create);
                addEmployeeWindow.AddSuccess += new EventHandler(AddEmployeeWindow_AddSuccess);

                addEmployeeWindow.ShowDialog();
            }
        }

        private void AddEmployeeWindow_AddSuccess(object sender, EventArgs e)
        {
            AddEmployeeWindow addEmployeeWindow = sender as AddEmployeeWindow;
            RS_EmployeeGroup selectedGroup = dtgGroup.SelectedItem as RS_EmployeeGroup;

            RS_Employee newEmployee = addEmployeeWindow.Employee;
            _groups.Find(delegate(RS_EmployeeGroup group)
            {
                if (group.GroupId == selectedGroup.GroupId)
                {
                    group.Employees.Add(newEmployee);
                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgEmployee, newEmployee);
        }

        private void btnModifyEmployee_Click(object sender, RoutedEventArgs e)
        {
            RS_EmployeeGroup selectedGroup = dtgGroup.SelectedItem as RS_EmployeeGroup;
            RS_Employee selectedEmployee = dtgEmployee.SelectedItem as RS_Employee;

            if (null != selectedGroup && null != selectedEmployee)
            {
                AddEmployeeWindow modifyEmployeeWindow = new AddEmployeeWindow(selectedGroup, _groups);
                modifyEmployeeWindow.Employee = selectedEmployee;
                modifyEmployeeWindow.InitializeWindow(CrudType.Update);
                modifyEmployeeWindow.AddSuccess += new EventHandler(modifyEmployeeWindow_AddSuccess);

                modifyEmployeeWindow.ShowDialog();
            }
        }

        void modifyEmployeeWindow_AddSuccess(object sender, EventArgs e)
        {
            var modifyEmployeeWindow = sender as AddEmployeeWindow;

            RS_Employee newEmployee = modifyEmployeeWindow.Employee;
            _groups.Find(delegate(RS_EmployeeGroup group)
            {
                if (group.GroupId == newEmployee.GroupId)
                {
                    group.Employees.Find(delegate(RS_Employee employee)
                    {
                        if (employee.UniqueId == newEmployee.UniqueId)
                        {
                            employee = newEmployee;
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    });

                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgEmployee, newEmployee);
        }

        private void btnDeleteEmployee_Click(object sender, RoutedEventArgs e)
        {

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeData();
        }

        private void dtgGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RS_EmployeeGroup group = dtgGroup.SelectedItem as RS_EmployeeGroup;
            if (null != group)
                ClientUtil.LoadItemsSource(dtgEmployee, group.Employees);
        }

        private void DataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            DataGrid dtg = sender as DataGrid;
            if (dtg.HasItems)
            {
                dtg.SelectedIndex = 0;
            }
        }
    }
}
